package com.yc.home.cloudother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.bean.Result;
import com.yc.entity.Classs;
import com.yc.entity.Createclass;
import com.yc.home.cloudother.mapper.ClasssMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzk
 * @since 2024-10-05
 */
@RestController
@RequestMapping("/classs")
public class ClasssController {

    @Autowired
    ClasssMapper classsMapper;

    //查询我加入的班级
    @RequestMapping("myin")
    Result myin(String id){
        List<Createclass> classinfo = classsMapper.classinfo(id);
        return new Result(1, "我创建的班级", classinfo);
    }

    //获取班级记录
    @RequestMapping("getRecord")
    Result getRecord(String id){
        List<Map<String, String>> record = classsMapper.getRecord(id);
        return new Result(1, "班级记录", record);
    }

    //查询我加入的班级
    @RequestMapping("getclass")
    Result getclass(String id, String search){
        List<Createclass> classinfo = classsMapper.getclass(id, search);
        return new Result(1, "我创建的班级", classinfo);
    }

    //修改名称
    @RequestMapping("updateSayName")
    Result updateSayName(String uid, String cid, String newName){
        try{
            LambdaQueryWrapper<Classs> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Classs::getUid, uid);
            lqw.eq(Classs::getCid, cid);
            Classs classs = classsMapper.selectOne(lqw);
            classs.setSayname(newName);
            classsMapper.updateById(classs);
            return new Result(1, "修改成功", null);
        }catch (Exception e){
            return new Result(0, "连接超时，请稍后再试！", null);
        }
    }

    //审核
    @RequestMapping("passOrNpass")
    Result passOrNpass(String id, int num){
        try{
            Classs classs = classsMapper.selectById(id);
            classs.setStatus(num);
            classsMapper.updateById(classs);
            return new Result(1, "审核成功", null);
        }catch (Exception e){
            return new Result(0, "连接超时，请稍后再试！", null);
        }
    }

    @Autowired
    RestTemplate restTemplate;

    //退出班级
    @Transactional
    @RequestMapping("exitOne")
    public Result exitOne(int uid, int cid){
        //退出班级
        LambdaQueryWrapper<Classs> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Classs::getUid, uid);
        lqw.eq(Classs::getCid, cid);
        classsMapper.delete(lqw);
        //去调用user模块的service层的删除方法
        // 调用user服务的API
        String userServiceUrl = "http://cloud-user/chat/one?uid=" + uid + "&cid=" + cid; // 确保URL正确，并且服务可访问

        ResponseEntity<Result> response = restTemplate.getForEntity(userServiceUrl, Result.class);

        // 处理user服务的响应
        if (response.getStatusCode().is2xxSuccessful()) {
            Result userServiceResult = response.getBody();
            if (userServiceResult != null) {
                // 根据userServiceResult做进一步处理
                System.out.println("userServiceResult = " + userServiceResult.getData());
            } else {
                // 处理user服务返回null的情况
                System.out.println("User service returned null response.");
            }
            return new Result(1, "退出成功", null);
        } else {
            // 处理调用失败的情况
            System.out.println("Failed to call user service. Status code: " + response.getStatusCode());
            return new Result(0, "调用用户服务失败", null);
        }
    }

    //查询在班级个人信息
    @RequestMapping("classinfo")
    Result classinfo(String cid, String uid){
        LambdaQueryWrapper<Classs> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Classs::getCid, cid);
        lqw.eq(Classs::getUid, uid);
        Classs classs = classsMapper.selectOne(lqw);

        return new Result(1, "班级个人信息", classs);
    }

}

